package leetcode.list;

/**
 * @author mazouri
 * @create 2021-08-18 18:45
 */
public class _19_removeNthFromEnd_dummyHead {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null) {
            return null;
        }

        ListNode cur = head;
        ListNode dummyHead = new ListNode(0, head);

        //求出链表长度
        int size = 0;
        while (cur != null) {
            size++;
            cur = cur.next;
        }

        cur = dummyHead;
        for (int i = 0; i < size - n; i++) {
            cur = cur.next;
        }

        cur.next = cur.next.next;
        return dummyHead.next;

    }
}
